package com.menu.Service.Impl;

import com.menu.Mapper.GoodsMapper;
import com.menu.Service.DataService;
import com.menu.Utils.CommonUtil;
import com.menu.Utils.RedisKeyUtils;
import com.menu.Utils.RedisUtil;
import com.menu.Utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DataServiceImpl implements DataService {
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public ResponseResult hourTrend() {
        Map<Object, Object> result = redisUtil.hmget(RedisKeyUtils.VISIT_COUNT_HOUR);
        if (result == null){
            return ResponseResult.error("获取失败！");
        }
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult fromPre(Integer state) {
        Map<String,Object> map = null;
        switch (state){
            case 0:
                map = goodsMapper.fromPreYesterday();
                break;
            case 1:
                map = goodsMapper.fromPreWeek();
                break;
            case 2:
                map = goodsMapper.fromPreMonth();
                break;
            case 3:
                map = goodsMapper.fromPreYear();
                break;
            default:
        }
        if (map != null && map.get("diff") == null){
            switch (state){
                case 0:
                    map.put("diff",map.get("today"));
                    break;
                case 1:
                    map.put("diff",map.get("week"));
                    break;
                case 2:
                    map.put("diff",map.get("month"));
                    break;
                case 3:
                    map.put("diff",map.get("year"));
                    break;
                default:
            }
        }
        if (map == null){
            map = new HashMap<>();
            switch (state){
                case 0:
                    map.put("today",0);
                    break;
                case 1:
                    map.put("week",0);
                    break;
                case 2:
                    map.put("month",0);
                    break;
                case 3:
                    map.put("year",0);
                    break;
                default:
            }
            map.put("diff",0);
        }
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult annualVisits() {
        Object object = redisUtil.get(RedisKeyUtils.ANNUAL_VISIT);
        if (object != null){
            List<Integer> list = (List<Integer>) object;
            return ResponseResult.success(list);
        }
        List<Integer> list = new ArrayList<>();
        Integer month = CommonUtil.getMonth();
        Integer year = CommonUtil.getYear();
        for (int i = 1; i <= 12; i ++) {
            if (i <= month){
                Integer temp = goodsMapper.countAnnual(0,i,year);
                if (temp == null ){
                    list.add(0);
                }else {
                    list.add(temp);
                }
            }else{
                list.add(0);
            }
        }
        redisUtil.set(RedisKeyUtils.ANNUAL_VISIT,list,60 * 60 * 2);
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult annualSales() {
        Object object = redisUtil.get(RedisKeyUtils.ANNUAL_SALES);
        if (object != null){
            List<Integer> list = (List<Integer>) object;
            return ResponseResult.success(list);
        }
        List<Integer> list = new ArrayList<>();
        Integer month = CommonUtil.getMonth();
        Integer year = CommonUtil.getYear();
        for (int i = 1; i <= 12; i ++) {
            if (i <= month){
                Integer temp = goodsMapper.countAnnual(1,i,year);
                if (temp == null ){
                    list.add(0);
                }else {
                    list.add(temp);
                }
            }else{
                list.add(0);
            }
        }
        redisUtil.set(RedisKeyUtils.ANNUAL_SALES,list,60 * 60 * 2);
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult turnoverRatio(Integer state) {
        Object object = null;
        switch (state){
            case 0:
                object = redisUtil.get(RedisKeyUtils.TURNOVER_RATIO_WEEK);
                break;
            case 1:
                object = redisUtil.get(RedisKeyUtils.TURNOVER_RATIO_MONTH);
                break;
            case 2:
                object = redisUtil.get(RedisKeyUtils.TURNOVER_RATIO_YEAR);
                break;
            default:
        }
        if (object != null){
            List<Double> list = (List<Double>) object;
            return ResponseResult.success(list);
        }
        List<Double> list = goodsMapper.selectByType(state);
        for (int i = list.size();i <= 12; i++){
            list.add(0.0);
        }
        switch (state){
            case 0:
                redisUtil.set(RedisKeyUtils.TURNOVER_RATIO_WEEK,list,60 * 60 * 24);
                break;
            case 1:
                redisUtil.set(RedisKeyUtils.TURNOVER_RATIO_MONTH,list,60 * 60 * 24);
                break;
            case 2:
                redisUtil.set(RedisKeyUtils.TURNOVER_RATIO_YEAR,list,60 * 60 * 24);
                break;
            default:
        }
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult allInSearch() {
        Set<Object> result = redisUtil.rangeZset(RedisKeyUtils.ALL_IN_SEARCH,0L,12L);
        return ResponseResult.success(result);
    }
}
